package com.wxproj.wxai.controller;


import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxproj.wxai.entity.UserText;
import com.wxproj.wxai.service.IUserTextService;
import com.wxproj.wxai.utils.AiManager;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.wxproj.wxai.utils.AiManager.STABLE_TEMPERATURE;
import static retrofit2.Response.success;

@RestController
@RequestMapping("/text")
public class UserTextController {
    @Autowired
    private IUserTextService userTextService;
    @Resource
    private AiManager aiManager;


    @GetMapping("/")
    public R takl(String content, HttpServletRequest httpServletRequest) throws Exception{
        Map<String, String> map = new HashMap<>();
        String val = "";
        try{
            Flowable vals = aiManager.doStreamRequest("小言",content,STABLE_TEMPERATURE);
            Flowable<String> mappedFlowable = vals.map(item -> "Number: " + item);
            // 订阅并处理数据
            mappedFlowable.subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.single())
                    .subscribe(
                            item -> {
                                System.out.println(item);
                                val.concat(item);
                            }, // onNext
                            error -> error.printStackTrace(),  // onError
                            () -> {
                                System.out.println("Done");
                                val.concat("Done");
                                String token = httpServletRequest.getHeader("Authorization").toString();
                                Claim claim = JWT.decode(token).getClaim("id");
                                String id = claim.toString().replaceAll("\"","");
                                UserText userText = new UserText();
                                userText.setContent(content);
                                userText.setUser_id(id);
                                userText.setCreate_time(new Date());
                                userTextService.save(userText);
                            }   // onComplete
                    );
            return R.ok("ok");
        } catch (Exception e){
            throw e;
        }

    }

    @PostMapping("/list")
    public R selectAll(Page<UserText> page, UserText userText) {
        return R.ok(this.userTextService.page(page, new QueryWrapper<>(userText)));
    }
}